
def recursion_area_rate(origin_data):
    for item in origin_data:
        origin_data[item] = origin_data[item] if int(origin_data[item]) < 100 else 100

    exclude_regions_keys = [key for key in origin_data.keys() if int(origin_data[key]) >= 100]
    include_regions_keys = [key for key in origin_data.keys() if int(origin_data[key]) < 100]
    all_rates = origin_data.values()
    all_full_rate = sum(int(num) for num in all_rates if int(num) >= 100)
    all_not_full_rate = sum(int(num) for num in all_rates if int(num) < 100)
    each_piece_rate = (int(16 * 100 - all_full_rate) / all_not_full_rate)

    has_full = False
    for key in include_regions_keys:
        origin_rate = origin_data[key]
        new_rate = int(origin_rate * each_piece_rate)
        if new_rate > 100:
            new_rate = 100
            origin_data[key] = new_rate
            has_full = True
            exclude_regions_keys.append(key)
    if has_full is False:
        return exclude_regions_keys , include_regions_keys, each_piece_rate
    else:
        return recursion_area_rate(origin_data)


def calculate_rate(origin, exclude_keys,include_keys,piece_rate):
    res = dict()
    for key in exclude_keys:
        res[key] = 100

    for key in include_keys:
        res[key] = int(int(origin[key]) * piece_rate)
    return res



data = dict()
data["3_1"] = 1
data["5_1"] = 2
data["6_1"] = 6
data["8_1"] = 8
data["9_1"] = 1
data["12_2"] = 109
data["12_3"] = 23
data["12_4"] = 16
data["13_1"] = 14
data["13_2"] = 50
data["13_3"] = 225
data["13_4"] = 1
data["14_1"] = 60
data["14_2"] = 6
data["14_3"] = 1
data["14_4"] = 8
data["15_1"] = 1
data["15_2"] = 1
data["15_3"] = 1
data["15_4"] = 1
data["18_1"] = 1
# {'6_1': 100, '8_1': 100, '12_2': 100, '12_3': 100, '12_4': 100, '13_1': 100, '13_2': 100, '13_3': 100, '14_1': 100, '14_2': 100, '14_4': 100, '3_1': 45, '5_1': 90, '9_1': 45, '13_4': 45, '14_3': 45, '15_1': 45, '15_2': 45, '15_3': 45, '15_4': 45, '18_1': 45}
# {'6_1': 100, '8_1': 100, '12_2': 100, '12_3': 100, '12_4': 100, '13_1': 100, '13_2': 100, '13_3': 100, '14_1': 100, '14_2': 100, '14_4': 100, '3_1': 45, '5_1': 90, '9_1': 45, '13_4': 45, '14_3': 45, '15_1': 45, '15_2': 45, '15_3': 45, '15_4': 45, '18_1': 45}
if len(data.keys()) <= 16:
    for key in data.keys():
        data[key] = 100
    res = data
else:
    exclude_regions_keys , include_regions_keys, each_piece_rate = recursion_area_rate(data)
    res = calculate_rate(data, exclude_regions_keys , include_regions_keys, each_piece_rate)
print(f"---res:{res}")